package com.huaxia.service.performance;

import com.huaxia.dao.performance.BranchPerformanceMapper;
import com.huaxia.dao.performance.BranchLongPerformanceMapper;
import com.huaxia.dao.performance.BranchReachMapper;
import com.huaxia.dao.performance.SaleDeptPerformanceMapper;
import com.huaxia.dao.performance.SaleDeptLongPerformanceMapper;
import com.huaxia.dao.performance.SaleDeptReachMapper;
import com.huaxia.dao.performance.*;
import com.huaxia.pojo.performance.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @author wangli 爱我华夏，爱我中华！
 * @date 2020/06/04 16:07
 */
@Service
public class PerformanceService {

    @Autowired
    private BranchPerformanceMapper branchPerformanceMapper;

    @Autowired
    private BranchLongPerformanceMapper branchLongPerformanceMapper;

    @Autowired
    private SaleDeptPerformanceMapper saleDeptDataMapper;

    @Autowired
    private SaleDeptLongPerformanceMapper saleDeptLongDataMapper;

    @Autowired
    private SzPerformanceMapper szDataMapper;

    @Autowired
    private SzLongPerformanceMapper szLongDataMapper;

    @Autowired
    private BranchReachMapper branchReachMapper;

    @Autowired
    private SaleDeptReachMapper saleDeptReachMapper;


    public List<SaleDeptPerformance> getSaleDeptPerformance() {
        return saleDeptDataMapper.getSaleDeptPerformanceThisMon();
    }
    public List<SaleDeptLongPerformance> getSaleDeptLongPerformance() {
        return saleDeptLongDataMapper.getSaleDeptLongPerformanceThisMon();
    }

    public List<SzPerformance> getSzPerformance() {
        return szDataMapper.getSzPerformanceThisMon();
    }

    public List<SzLongPerformance> getSzLongPerformance() {
        return szLongDataMapper.getSzLongPerformanceThisMon();
    }

    //查询下月数据
    public List<BranchPerformance> getBranchPerformanceNextMon() {

        List<Map> mapList = branchPerformanceMapper.findYxTargetNextMon();
        List<BranchPerformance> branchPerformances = branchPerformanceMapper.getBranchPerformanceNextMon();

        if (mapList != null && mapList.size() > 0) {
            for (BranchPerformance branchPerformance : branchPerformances) {
                for (Map map : mapList) {
                    if (map.get("BRANCHSNAME").equals(branchPerformance.getBranchsName())) {
                        branchPerformance.setTarget(map.get("GOAL") + "");
                        break;
                    }
                }
            }
        }
        branchPerformances.sort((a, b) -> b.getBranchMonthAdvance().compareTo(a.getBranchMonthAdvance()));
        return branchPerformances;
    }

    //查询本月数据
    public List<BranchPerformance> getBranchPerformanceThisMon() {


        List<Map> mapList = branchPerformanceMapper.findYxTargetThisMon();
        //获取今天是几号
        int nowDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        List<BranchPerformance> branchPerformances = branchPerformanceMapper.getBranchPerformanceThisMon();

        if (mapList != null && mapList.size() > 0) {
            for (BranchPerformance branchPerformance : branchPerformances) {
                for (Map map : mapList) {
                    if (map.get("BRANCHSNAME").equals(branchPerformance.getBranchsName())) {
                        branchPerformance.setTarget(map.get("GOAL") + "");
                        break;
                    }
                }
            }
        }


        if (nowDay <= 15) {
            branchPerformances.sort((a, b) -> b.getBranchMonthAdvance().compareTo(a.getBranchMonthAdvance()));
        } else {
            branchPerformances.sort((a, b) -> b.getBranchMonthInSure().compareTo(a.getBranchMonthInSure()));
        }

        return branchPerformances;
    }

    //查询下月期交数据
    public List<BranchLongPerformance> getBranchLongPerformanceNextMon() {

        List<Map> mapList = branchLongPerformanceMapper.findYxTargetNextMon();
        List<BranchLongPerformance> branchLongPerformances = branchLongPerformanceMapper.getBranchLongPerformanceNextMon();

        if (mapList != null && mapList.size() > 0) {
            for (BranchLongPerformance branchLongPerformance : branchLongPerformances) {
                for (Map map : mapList) {
                    if (map.get("BRANCHSNAME").equals(branchLongPerformance.getBranchsName())) {
                        branchLongPerformance.setTarget(map.get("GOAL") + "");
                        break;
                    }
                }
            }
        }
        branchLongPerformances.sort((a, b) -> b.getBranchMonthAdvance().compareTo(a.getBranchMonthAdvance()));
        return branchLongPerformances;
    }

    //查询本月期交数据
    public List<BranchLongPerformance> getBranchLongPerformanceThisMon() {


        List<Map> mapList = branchLongPerformanceMapper.findYxTargetThisMon();
        //获取今天是几号
        int nowDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        List<BranchLongPerformance> branchLongPerformances = branchLongPerformanceMapper.getBranchLongPerformanceThisMon();

        if (mapList != null && mapList.size() > 0) {
            for (BranchLongPerformance branchLongPerformance : branchLongPerformances) {
                for (Map map : mapList) {
                    if (map.get("BRANCHSNAME").equals(branchLongPerformance.getBranchsName())) {
                        branchLongPerformance.setTarget(map.get("GOAL") + "");
                        break;
                    }
                }
            }
        }


        if (nowDay <= 15) {
            branchLongPerformances.sort((a, b) -> b.getBranchMonthAdvance().compareTo(a.getBranchMonthAdvance()));
        } else {
            branchLongPerformances.sort((a, b) -> b.getBranchMonthInSure().compareTo(a.getBranchMonthInSure()));
        }

        return branchLongPerformances;
    }


    public List<BranchReach> getBranchReach() {
        return branchReachMapper.getBranchReach();
    }

    public List<SaleDeptReach> getSaleDeptReach() {
        return saleDeptReachMapper.getSaleDeptReach();
    }


    public Boolean findYxTargetThisMon() {
        List<Map> yxTargetThisMon = branchPerformanceMapper.findYxTargetThisMon();
        return yxTargetThisMon == null || yxTargetThisMon.size() <= 0;
    }

    public Boolean findYxTargetNextMon() {
        List<Map> yxTargetNextMon = branchPerformanceMapper.findYxTargetNextMon();
        return yxTargetNextMon == null || yxTargetNextMon.size() <= 0;
    }
}
